package com.salon.modules.sys.controller;

import com.salon.common.annotation.SysLog;
import com.salon.common.http.response.ObjectResult;
import com.salon.common.http.response.PageResult;
import com.salon.common.validator.ValidatorUtils;
import com.salon.modules.sys.entity.SysConfigEntity;
import com.salon.modules.sys.service.ISysConfigService;
import java.util.Map;
import javax.annotation.Resource;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

/**
 * 系统参数信息
 */
@RestController
@RequestMapping("/sys/config")
@Api(tags="配置管理")
public class SysConfigController extends BaseController {

    @Resource
    private ISysConfigService sysConfigService;

    @GetMapping("/list")
    @ApiOperation("分页")
    @RequiresPermissions("sys:config:select")
    public PageResult<SysConfigEntity> list(@ApiIgnore @RequestParam Map<String, Object> params) {
        return sysConfigService.listByPage(params);
    }

    @GetMapping("/info/{id}")
    @ApiOperation("详情")
    @RequiresPermissions("sys:config:select")
    public ObjectResult<SysConfigEntity> info(@PathVariable("id") Long id) {
        SysConfigEntity config = sysConfigService.getById(id);
        return ObjectResult.success(config);
    }

    @SysLog("保存参数")
    @PostMapping("/save")
    @ApiOperation("保存")
    @RequiresPermissions("sys:config:save")
    public ObjectResult<String> save(@RequestBody SysConfigEntity config) {
        ValidatorUtils.validateEntity(config);
        sysConfigService.saveConfig(config);
        return ObjectResult.success();
    }

    @SysLog("修改参数")
    @PostMapping("/update")
    @ApiOperation("修改")
    @RequiresPermissions("sys:config:update")
    public ObjectResult<String> update(@RequestBody SysConfigEntity config) {
        ValidatorUtils.validateEntity(config);
        sysConfigService.update(config);
        return ObjectResult.success();
    }

    @SysLog("删除参数")
    @PostMapping("/delete")
    @ApiOperation("删除")
    @RequiresPermissions("sys:config:delete")
    public ObjectResult<String> delete(@RequestBody Long[] ids) {
        sysConfigService.deleteBatch(ids);
        return ObjectResult.success();
    }

}
